import { AllSkillKey, IMDSkillConfig, PlayerSkillField, SkillLearnType } from "../../config/SkillConfig";
import SkillUICreator from "../../ui/skill/SkillUICreator";
import L_Run from "../../utils/L_Run";
import L_String from "../../utils/L_String";
import HeroLearnSkill from "./HeroLearnSkill";
import PlayerLearnedSingleSkill from "./PlayerLearnedSingleSkill";


type PlayerSkillFieldExpand=PlayerSkillField & {
  name:string,
  iconPath:string,
  desc:string
}

/** 玩家学习到的所有技能集合 
 * 
 * 玩家->学到技能map(含等级管理)->共享分配给玩家的英雄
 * 
 * 
*/
export default class PlayerLearnedSkillsManager{

  //<玩家索引,<已学习技能类型,已学习技能集合>>
  private static allPlayerSkillsLearnMap:Map<number,Map<SkillLearnType,PlayerLearnedSingleSkill[]>> = new Map<number,Map<SkillLearnType,PlayerLearnedSingleSkill[]>>();
 
  //玩家可学技能的英雄集合
  private static playerLKHerosMap:Map<number,HeroLearnSkill[]> = new Map<number,HeroLearnSkill[]>();

  
  // constructor(){
                  
  // }

  /**初始化注册玩家可以学习技能的英雄  初始化不给任何技能   先不用初始化处理思路  使用playerSkillLearnAndHeroDataSync的智能单位绑定功能 */
  // public static initPlayerSkillLearnHero(playerIndex:number,hero:unit){
  //   let playerHeros = PlayerLearnedSkillsManager.playerLKHerosMap.get(playerIndex);
  //   if(playerHeros==undefined || playerHeros.length==0 || playerHeros.filter(c=>c.heroUnit==hero).length==0){
  //     playerHeros?.push(new HeroLearnSkill(hero))
  //   }
  // }

  /**
   * 设置玩家可学习技能后 同步关联英雄的技能等级
   * @param playerIndex 玩家索引
   * @param skillIdStr 技能id
   * @param initSkillExp 学习经验值 如果只做技能改变 可以设置此参数为0
   * @param selectHero 指定英雄 (注意 指定英雄是带有强制改变类型技能功能的  如果不选择英雄 只会视为玩家技能等级的提高会去跟踪一下关联等级的更新)
   */
  public static playerSkillLearnAndSyncHeroData(playerIndex:number,skillIdStr:AllSkillKey,initSkillExp:number,selectHero?:unit){


    //-----------1.刷新玩家可学习技能的管理数据--------------
    let playerSkillLearnMapT = this.allPlayerSkillsLearnMap.get(playerIndex);
    if(playerSkillLearnMapT==undefined){
      playerSkillLearnMapT = new Map<SkillLearnType,PlayerLearnedSingleSkill[]>();
      this.allPlayerSkillsLearnMap.set(playerIndex,playerSkillLearnMapT);
    }
    let skillParams=PlayerLearnedSkillsManager.getSkillSimpleAttr(skillIdStr);
    if(skillParams==undefined){
      console.log("[ERROR]配置异常!",skillIdStr,"找不到属性配置!")
      return
    }

    let typeSkillArr=playerSkillLearnMapT.get(skillParams.lType)
    if(typeSkillArr==undefined){
      typeSkillArr=[];
      playerSkillLearnMapT.set(skillParams.lType,typeSkillArr);
    }
    let targetLearnSkillObj=typeSkillArr?.find(c=>c.skillId==skillIdStr)
    if(targetLearnSkillObj ==undefined){
      //从未学习过的技能 添加 激活与否查看是否有同类已激活技能
      let activateFlag=typeSkillArr!.find(c=>c.activate==true)!=undefined
      targetLearnSkillObj=new PlayerLearnedSingleSkill(skillIdStr,skillParams.name,skillParams.lType,initSkillExp,skillParams.lvlUpExpParams,skillParams.iconPath,skillParams.desc,activateFlag)
      targetLearnSkillObj.skillLvlup(initSkillExp)
      typeSkillArr!.push(targetLearnSkillObj)
      //刷新技能面板  新增技能
      SkillUICreator.uiAddPlayerLenardSkill(playerIndex,targetLearnSkillObj)
    }else{
      //已经学习过的技能 直接升级
      targetLearnSkillObj.skillLvlup(initSkillExp)
      //刷新技能面板  同步技能等级
      SkillUICreator.uiSyncPlayerLenardSkillLvl(playerIndex,targetLearnSkillObj)

    }

    // console.log(">>>>>玩家技能管理map>>>>",L_String.getObjJSONStr(PlayerLearnedSkillsManager.allPlayerSkillsLearnMap))
    // console.log(">>>>玩家英雄技能管理map>>>>",L_String.getObjJSONStr(PlayerLearnedSkillsManager.playerLKHerosMap))
    

    //----------2.改变玩家可学习技能数据后  同步玩家英雄的技能等级----------
    this.syncHeroSkillData(playerIndex,skillIdStr,targetLearnSkillObj.skillLvl,skillParams.lType,selectHero)

    //----------3.以后也可以作为天赋学习的拓展 和技能是一样的  复制HeroLearnSkill.ts 参考此管理方式-----
    
    
  }

  /**同步玩家可学习技能英雄的数据  单独抠一个方法是为了解耦 */
  private static syncHeroSkillData(playerIndex:number,skillIdStr:AllSkillKey,skillLvL:number,lType:SkillLearnType,selectHero?:unit){
    let playerHeros = PlayerLearnedSkillsManager.playerLKHerosMap.get(playerIndex);
    if(playerHeros===undefined){
      playerHeros=[]
      PlayerLearnedSkillsManager.playerLKHerosMap.set(playerIndex,playerHeros)
    }
    if(selectHero!=undefined){
      //指定了单位 做改变技能同步逻辑
      let heroLK=playerHeros.find(v=>v.heroUnit==selectHero)
      if(heroLK==undefined){
        //指定的单位完全没有做初始化
        heroLK=new HeroLearnSkill(selectHero)
        playerHeros.push(heroLK)
      }
      heroLK.syncTypeSkill(skillIdStr,skillLvL,lType)
    }else{
      //不指定单位 跟踪一下所有可能存在的技能等级同步
      playerHeros.forEach(v=>{
        v.syncTypeSkillLvl(skillIdStr,skillLvL,lType)
      })
    }

  }

  


   /**找到技能的学习类型*/
   private static getSkillSimpleAttr(skillIdStr:string):PlayerSkillFieldExpand | undefined{
    let skillConfig=IMDSkillConfig[skillIdStr]

    //TODO
    if(skillConfig!=undefined && skillConfig.playerSkillField!=undefined){
       return {'lType':skillConfig.playerSkillField!.lType,'lvlUpExpParams':skillConfig.playerSkillField!.lvlUpExpParams,name:skillConfig.name,'iconPath':(skillConfig.learnIconPath || ''),'desc':(skillConfig.desc || '')}
    }else{
       return undefined
    }  
  }


  
}  